﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;

namespace GameSchool.Controllers
{
    public class LectureController : Controller
    {
        GameSchoolRepository repository = new GameSchoolRepository();
        

        //
        // GET: /Lecture/

        #region Views

        //For teachers and students
       [Authorize(Roles="Students")]
        public ActionResult ViewLectureStudent(int CID, int LID)
       {
           StudentLectureViewModel sViewModel = new StudentLectureViewModel();
           Lecture currentLecture = repository.GetLectureByID(LID);
           var studentLogin = repository.GetStudentLoginRelationship();
           sViewModel.loggedInStudent = studentLogin;
           var comments = repository.GetComments();
           sViewModel.sidebarCourses = repository.GetCoursesByStudent(studentLogin).ToList(); //for master view model
           sViewModel.studentName = studentLogin.name; //For master view model
           sViewModel.comments = comments.ToList();
           sViewModel.currentLecture = currentLecture;

           return View(sViewModel);
        }

       //For students to view all lectures in a course
        [Authorize(Roles = "Students")]
       public ActionResult ViewLecturesInCourse(int CID)
       {
           StudentLectureViewModel sViewModel = new StudentLectureViewModel();
           sViewModel.itemList = new List<StudentLectureViewModel.LevelAndTasksItem>();

           var studentLogin = repository.GetStudentLoginRelationship();
           var theCourse = repository.GetCourseByID(CID);
           sViewModel.sidebarCourses = repository.GetCoursesByStudent(studentLogin).ToList(); //for master view model
           sViewModel.studentName = studentLogin.name; //For master view
           sViewModel.courseName = repository.GetCourseByID(CID).title;
           sViewModel.courseID = CID;
           sViewModel.levels = repository.GetAllLevelsByCourse(theCourse);
           foreach (var item in sViewModel.levels)
           {
               sViewModel.itemList.Add(new StudentLectureViewModel.LevelAndTasksItem
               {
                   levelID = item.levelID,
                   levelTitle = item.title,
                   levelNumber = item.number,
                   levelXP = item.intLevelXP,
                   lectureID = repository.GetLectureByLevel(item).lectureID
               });
           }

           return View(sViewModel);
       }

        [Authorize(Roles="Teachers")]
        public ActionResult ViewLectureTeacher(int CID, int LID)
        {
            LectureViewModel viewmodel = new LectureViewModel();
            Lecture currentLecture = repository.GetLectureByID(LID);
            var teacherLogin = repository.GetTeacherLoginRelationship();
            viewmodel.loggedInTeacher = teacherLogin;
            var comments = repository.GetComments();
            viewmodel.sidebarCourses = repository.GetCoursesByTeacher(teacherLogin).ToList();
            viewmodel.teacherName = teacherLogin.name;     
            viewmodel.comments = comments.ToList();
            viewmodel.currentLecture = currentLecture;

            return View(viewmodel);
        }

        [HttpPost]
        public ActionResult ViewLectureTeacher(FormCollection formData)
        {
            Comment c = new Comment();

            var teacherLogin = repository.GetTeacherLoginRelationship();

            c.userName = teacherLogin.name;
            c.teacherID = teacherLogin.teacherID;
            c.lectureID = Convert.ToInt32(formData["lectureID"]);
            c.commentBody = formData["commentBody"];
            c.CommentTime = DateTime.Now;

            repository.AddComment(c);
            repository.Save();

            return Redirect("~/Lecture/ViewLectureTeacher");
        }

        //For teachers
        public ActionResult LectureEdit(int CID, int LVID)
        {
            LectureViewModel viewmodel = new LectureViewModel();
            var order = repository.GetAllLevelsByCourse(CID).ToList();
            var currentLectureID = repository.GetLectureByLevelID(LVID).lectureID;
            var teacherLogin = repository.GetTeacherLoginRelationship();
            viewmodel.loggedInTeacher = teacherLogin;
            viewmodel.sidebarCourses = repository.GetCoursesByTeacher(teacherLogin).ToList();
            viewmodel.teacherName = teacherLogin.name;  
            viewmodel.levels = order;
            viewmodel.levelID = LVID;
            viewmodel.courseID = CID;
            viewmodel.lectureID = currentLectureID;
            viewmodel.lectureXP = repository.GetLectureByID(currentLectureID).lectureXP;
            viewmodel.lec = repository.GetLectureByLevelID(LVID);
            viewmodel.lectureFile = viewmodel.lec.lectureFile;
            viewmodel.lectureDescription = viewmodel.lec.description;
            return View(viewmodel);
        }

        [HttpPost]
        public ActionResult LectureEdit(FormCollection formData)
        {
        
            Lecture currentLecture = new Lecture();

            currentLecture.levelID = Convert.ToInt32(formData["levelID"]);
            var courseID = Convert.ToInt32(formData["courseID"]);
            var lectureID = Convert.ToInt32(formData["lectureID"]);
            currentLecture.lectureXP = Convert.ToDouble(formData["lectureXP"]);
            currentLecture.title = formData["lectureTitle"];
            currentLecture.description = formData["lectureDescription"];
            currentLecture.lectureFile = formData["lectureFile"];
            //currentLecture.allowApproval = Convert.ToBoolean(formData["lectureAllowApproval"]);
            currentLecture.approvalXPAmount = Convert.ToInt32(formData["lectureApprovalXPAmount"]);
            currentLecture.likeAmountToGainXP = Convert.ToInt32(formData["lectureLikeAmountToGainXP"]);
            currentLecture.likeXPAmount = Convert.ToInt32(formData["lectureLikeXPAmount"]);
            currentLecture.lectureID = lectureID;
            
          
            repository.UpdateLecture(currentLecture);
            repository.Save();

			return RedirectToAction("../Lecture/ViewLectureTeacher", new { CID = courseID, LID = lectureID });

        }

        //For teachers
        public ActionResult AssignScoreRules()
        {
            return View();
        }

        #endregion

        public Notification GetNotifications(Notification theNotification)
        {
            return theNotification;
        }

    }
}
